﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataTransferObject;
using DataTransferObject.DiaryAndTrackingTask;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
using DataTransferObject.DiaryAndTrackingTask.SupportClassForDiary;
using DataTransferObject.DiaryAndTrackingTask.SupportClassForUpdateTask;



namespace DataService.LocalFile
{
    public class LocalFileDataServiceImpl : IDataService
    {

        #region IDataService Members

        public ListEvent ReadListEvent()
        {
            ListEvent loadedListEvent;

            String pathToLoad = Path.Combine(Environment.CurrentDirectory, @".\ListEvent.bin");

            FileStream fs = new FileStream(pathToLoad, FileMode.OpenOrCreate);
            if (fs.Length == 0)
            {
                return new ListEvent();
            }
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the hashtable from the file and
                // assign the reference to the local variable.
                loadedListEvent = (ListEvent)formatter.Deserialize(fs);
                return loadedListEvent;
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + ex.Message);
            }
            finally
            {
                fs.Close();
            }
            return null;
        }

        public void WriteListEvent(ListEvent _ListEvent)
        {
            String pathToSave = Path.Combine(Environment.CurrentDirectory, @".\ListEvent.bin");
            FileStream stream = new FileStream(pathToSave, FileMode.Create);

            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                formatter.Serialize(stream, _ListEvent);
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to serialize. Reason: " + ex.Message);
                throw;
            }
            finally
            {
                stream.Close();
            }
        }

        public AnswerList ReadAnswerList()
        {
            AnswerList loadedAnswerList;

            String pathToLoad = Path.Combine(Environment.CurrentDirectory, @".\AnswerList.bin");

            FileStream fs = new FileStream(pathToLoad, FileMode.OpenOrCreate);
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the hashtable from the file and
                // assign the reference to the local variable.
                loadedAnswerList = (AnswerList)formatter.Deserialize(fs);
                fs.Close();
                return loadedAnswerList;
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + ex.Message);
            }
            finally
            {
                fs.Close();
            }
            return null;
        }

        public void WriteAnswerList(AnswerList _AnswerList)
        {
            String pathToSave = Path.Combine(Environment.CurrentDirectory, @".\AnswerList.bin");
            FileStream stream = new FileStream(pathToSave, FileMode.Create);

            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                formatter.Serialize(stream, _AnswerList);
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to serialize. Reason: " + ex.Message);
                throw;
            }
            finally
            {
                stream.Close();
            }
        }

        public DiaryDS ReadDiaryDS()
        {
            DiaryDS loadedDiaryDS = null;

            String pathToLoad = Path.Combine(Environment.CurrentDirectory, @".\DiaryDS.bin");

            FileStream fs = new FileStream(pathToLoad, FileMode.OpenOrCreate);

            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the hashtable from the file and
                // assign the reference to the local variable.
                loadedDiaryDS = (DiaryDS)formatter.Deserialize(fs);
                loadedDiaryDS.Analysis = new AnalysisDS();

            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + ex.Message);
            }
            finally
            {
                fs.Close();
            }
            return loadedDiaryDS == null ? new DiaryDS() : loadedDiaryDS;
        }

        public void WriteDiaryDS(DiaryDS _DiaryDS)
        {
            String pathToSave = Path.Combine(Environment.CurrentDirectory, @".\DiaryDS.bin");
            FileStream stream = new FileStream(pathToSave, FileMode.Create);
            _DiaryDS.Analysis = new AnalysisDS();
            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                formatter.Serialize(stream, _DiaryDS);
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to serialize. Reason: " + ex.Message);
                throw;
            }
            finally
            {
                stream.Close();
            }
        }

        public TrackingTaskDS ReadTrackingTaskDS()
        {
            TrackingTaskDS loadedTrackingTaskDS;

            String pathToLoad = Path.Combine(Environment.CurrentDirectory, @".\TrackingTaskDS.bin");

            FileStream fs = new FileStream(pathToLoad, FileMode.OpenOrCreate);
            if (fs.Length == 0)
            {
                fs.Close();
                return new TrackingTaskDS();
            }
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the hashtable from the file and
                // assign the reference to the local variable.
                loadedTrackingTaskDS = (TrackingTaskDS)formatter.Deserialize(fs);
                loadedTrackingTaskDS.BurndownGraphDataFromTo = new List<TaskBurndown>();
                return loadedTrackingTaskDS;
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + ex.Message);
            }
            finally
            {
                fs.Close();
            }
            return null;
        }

        public void WriteTrackingTaskDS(TrackingTaskDS _TrackingTaskDS)
        {
            String pathToSave = Path.Combine(Environment.CurrentDirectory, @".\TrackingTaskDS.bin");
            FileStream stream = new FileStream(pathToSave, FileMode.Create);

            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                formatter.Serialize(stream, _TrackingTaskDS);
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to serialize. Reason: " + ex.Message);
                throw;
            }
            finally
            {
                stream.Close();
            }
        }

        public ListEvent ReadListSchedEvent()
        {
            ListEvent loadedListEvent;

            String pathToLoad = Path.Combine(Environment.CurrentDirectory, @".\ListSchedEvent.bin");

            FileStream fs = new FileStream(pathToLoad, FileMode.OpenOrCreate);
            if (fs.Length == 0)
            {
                return new ListEvent();
            }
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the hashtable from the file and
                // assign the reference to the local variable.
                loadedListEvent = (ListEvent)formatter.Deserialize(fs);
                return loadedListEvent;
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + ex.Message);
            }
            finally
            {
                fs.Close();
            }
            return null;
        }

        public void WriteListSchedEvent(ListEvent _ListEvent)
        {
            String pathToSaveSched = Path.Combine(Environment.CurrentDirectory, @".\ListSchedEvent.bin");
            FileStream streamSched = new FileStream(pathToSaveSched, FileMode.Create);

            BinaryFormatter formatterSched = new BinaryFormatter();
            try
            {
                formatterSched.Serialize(streamSched, _ListEvent);
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to serialize. Reason: " + ex.Message);
                throw;
            }
            finally
            {
                streamSched.Close();
            }
        }

        public SystemConfiguration ReadFromConfigFile()
        {
            SystemConfiguration loadedSysConfig;
            String pathToLoad = Path.Combine(Environment.CurrentDirectory, @".\JSKEData.bin");
            FileStream fs = new FileStream(pathToLoad, FileMode.OpenOrCreate);
            if (fs.Length == 0)
            {
                fs.Close();
                loadedSysConfig = SystemConfiguration.getInstance();
                return loadedSysConfig;
            }
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the hashtable from the file and
                // assign the reference to the local variable.
                loadedSysConfig = (SystemConfiguration)formatter.Deserialize(fs);
                fs.Close();
                return loadedSysConfig;
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + ex.Message);
                //throw ex;
            }
            finally
            {
                fs.Close();
            }
            return null;
        }

        public bool WriteToConfigFile(SystemConfiguration result)
        {
            String pathToSave = Path.Combine(Environment.CurrentDirectory, @".\JSKEData.bin");
            FileStream stream = new FileStream(pathToSave, FileMode.Create);

            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                formatter.Serialize(stream, result);
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to serialize. Reason: " + ex.Message);
                throw;
            }
            finally
            {
                stream.Close();
            }
            return true;
        }

        public ScheduleReport readScheduleReport()
        {
            ScheduleReport loadedSchedReport = new ScheduleReport();

            String pathToLoad = Path.Combine(Environment.CurrentDirectory, @".\ScheduleReport.bin");

            FileStream fs = new FileStream(pathToLoad, FileMode.OpenOrCreate);
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the hashtable from the file and
                // assign the reference to the local variable.
                loadedSchedReport = (ScheduleReport)formatter.Deserialize(fs);                
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + ex.Message);
            }
            finally
            {
                fs.Close();
            }
            return loadedSchedReport;
        }

        public bool writeSchedReport(ScheduleReport result)
        {
            String pathToSave = Path.Combine(Environment.CurrentDirectory, @".\ScheduleReport.bin");
            //String pathToSave = Environment.CurrentDirectory + "\\ScheduleReport.bin";
            FileStream stream = new FileStream(pathToSave, FileMode.Create);

            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                formatter.Serialize(stream, result);
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to serialize. Reason: " + ex.Message);
                throw;
            }
            finally
            {
                stream.Close();
            }
            return true;
        }

        public TaskList readTaskList()
        {
            TaskList loadedTaskList = new TaskList();
            String pathToLoad = Path.Combine(Environment.CurrentDirectory, @".\TaskList.bin");

            FileStream fs = new FileStream(pathToLoad, FileMode.OpenOrCreate);
            if (fs.Length == 0)
            {
                return loadedTaskList;
            }
            try
            {
                BinaryFormatter formatter = new BinaryFormatter();

                // Deserialize the hashtable from the file and
                // assign the reference to the local variable.
                loadedTaskList = (TaskList)formatter.Deserialize(fs);
                //MessageBox.Show("size taskList is: " + Convert.ToString(loadedTaskList.GetTaskList.Count));
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to deserialize. Reason: " + ex.Message);
            }
            finally
            {
                fs.Close();
            }
            return loadedTaskList;
        }

        public bool writeTaskList(TaskList result)
        {
            String pathToSave = Path.Combine(Environment.CurrentDirectory, @".\TaskList.bin");
            FileStream stream = new FileStream(pathToSave, FileMode.Create);
            BinaryFormatter formatter = new BinaryFormatter();
            try
            {
                formatter.Serialize(stream, result);
                //MessageBox.Show("stream size = "+Convert.ToString(stream.Length)+ ", numOf Task = " + Convert.ToString(result.GetTaskList.Count));
                
            }
            catch (SerializationException ex)
            {
                Console.WriteLine("Failed to serialize. Reason: " + ex.Message);
                throw;
            }
            finally
            {
                stream.Close();
            }
            return true;
        }

        #endregion
    }
}
